<?php

class Keywords extends MY_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
        $this->load->library('CacheHelper');
        $this->load->service('Service_keywords');
        $this->load->service('Service_urls');
    }

    public function index()
    {
        $params = $this->getParams();
        $key = isset($params['key']) ? $params['key'] : '';
        $page = !empty($params['page']) ? ((int)$params['page'] > 0 ? (int)$params['page'] : 1) : 1;

        $cond = ['status' => 0];
        $list = $this->Service_keywords->getList($page, $cond, 24);
        $pagination = $this->Service_keywords->getPagination($page, $cond, 24);
        $pagination['url'] = !empty($key) ? '/keywords?key=' . $key : '/';

        $this->assign('tag', $this->cateMapping['keywords']);
        $this->assign('title', $this->cateMapping['keywords']);
        $this->assign('key', $key);
        $this->assign('list', $list);
        $this->assign('pagination', $pagination);
        $this->display('keywords/index.tpl');
    }

    public function detail($id = 13)
    {
        $id = (int)$id;
        $info = $this->Service_keywords->get($id);
        if ($info['status'] == -1) {// 删除跳转到特殊页
            redirect('', 'auto', 404);
        }

        $urls = $this->Service_keywords->getListByKeyword(1, ['keywords`.`id' => $id, 'urls`.`status' => 0, 'urls`.`show' => 1], 100);

        $description = (strlen($info['keyword']) <= 25 ? ($info['keyword'] . " " . $this->site['desc']) : StrHelper::cutstr($info['keyword'], 150));
        $this->assign('tag', $info['keyword']);
        $this->assign('title', $info['keyword']);
        $this->assign('keywords', $info['keyword']);
        $this->assign('description', $description);
        $this->assign('info', $info);
        $this->assign('urls', $urls);
        $this->display('keywords/detail.tpl');
    }
}
